package com.csair.seam.infrastructure.web;

import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.csair.seam.infrastructure.sso.CustomSsoActor;
import com.csair.seam.infrastructure.utils.JwtUtil;
import com.csair.seam.infrastructure.utils.WebUtil;

/**
 * @Author: wurunbin
 * @Date: 2020/3/5 11:35
 * @Description:
 **/
public class BaseController{


    protected <T> BaseResponse<T> buildSuccess(T data){
        return new BaseResponse<>(ResponseCode.SUCCESS,
                "成功",data);
    }

    protected <T> BaseResponse<T> buildError(T data){
        return new BaseResponse<>(ResponseCode.BIZ_LIMIT,
                "失败",data);
    }

    protected <T> BaseResponse<T> buildError(String message,T t){
        return new BaseResponse<>(ResponseCode.BIZ_LIMIT,message,t);
    }

    /**
     * @return 员工登录账号（员工号）
     */
    protected String getAccount() {
    	HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    	return (String) Objects.requireNonNull(request.getSession().getAttribute(CustomSsoActor.SESSION_KEY_ACCOUNT));
    }
    
    protected String getAccountForJwt() {
    	HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    	return JwtUtil.getAccount(request);
    }
    
    /**
	 * 获取IP地址
	 * @author 蓝润利
	 * @param request
	 * @return
	 */
	protected String getIpAddress() {
		HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        return WebUtil.getIpAddress(request);
	}


}
